<script setup>
import { login } from '@/api/user'
import { useUserStore } from '@/stores'
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const userInfo = ref({
  username: 'admin',
  password: '123456',
})
const userStore = useUserStore()
const formRef = ref(null)
// 表单验证规则
const formRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 10, message: '长度在3到10个字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 15, message: '长度在6到15个字符', trigger: 'blur' },
  ],
}

// 重置表单
function resetFrom() {
  formRef.value.resetFields()
}
const router = useRouter()
// 登录按钮
async function loginFun() {
  await formRef.value.validate()
  const { data: res } = await login(userInfo.value)
  // console.log(res)
  if (res.meta.status !== 200) {
    // eslint-disable-next-line no-undef
    return ElMessage.error('登录失败')
  }
  // 将得到的token存储到仓库中
  // eslint-disable-next-line no-undef
  ElMessage.success('登录成功')
  userStore.setToken(res.data.token)
  router.push('/home')
}
</script>

<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.svg" alt="">
      </div>
      <!-- 登录表单区域 -->
      <el-form ref="formRef" :rules="formRules" :model="userInfo" label-width="0px" class="login_form">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="userInfo.username">
            <template #prefix>
              <i class="iconfont icon-users" />
            </template>
          </el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input v-model="userInfo.password" type="password" show-password>
            <template #prefix>
              <i class="iconfont icon-24gl-lock2" />
            </template>
          </el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="loginFun">
            登录
          </el-button>
          <el-button type="info" @click="resetFrom">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login_container {
  height: 100%;
  background-color: #2b4b6b;

  .login_box {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    transform: translate(-50%, -50%);

    .login_form {
      box-sizing: border-box;
      display: flex;
      flex-direction: column;
      justify-content: center;
      width: 100%;
      height: 100%;
      padding: 0 20px;
      margin-top: 10px;

      .btns {
        display: flex;
        justify-content: space-between;
      }
    }
  }
}

.avatar_box {
  position: absolute;
  left: 50%;
  width: 130px;
  height: 130px;
  padding: 10px;
  background-color: #fff;
  border: 1px solid #eee;
  border-radius: 50%;
  box-shadow: 0 0 10px #ddd;
  transform: translate(-50%, -50%);

  img {
    width: 100%;
    height: 100%;
    background-color: #eee;
    border-radius: 50%;
  }
}
</style>
